home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / tabtrick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  1.6 KB  |  56 lines

  1. /*
  2. **  TABTRICKs.C - Demonstrates how to use printf() for columnar formatting
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define putnum(i) putchar(i+'0')
  9.  
  10. main()
  11. {
  12.       char *firstname[] = { "Aloysius",   "Bob",   "Dennis",  NULL },
  13.            *lastname[]  = { "Fuddrucker", "Stout", "Ritchie", NULL };
  14.       int   score[]     = { -10,          70,      200,       0    },
  15.             i, tabwidth;
  16.  
  17.       printf("%15sStudent Name%30s\n\n", "", "Test Score");
  18.       for (i = 0; NULL != lastname[i]; ++i)
  19.       {
  20.             tabwidth = 36                             /* spaces to tab  */
  21.                        -2                             /* allow for ", " */
  22.                        -strlen(lastname[i]);          /* lastname space */
  23.             printf("%15s%s, %-*s%3d\n",
  24.                   "", lastname[i], tabwidth, firstname[i], score[i]);
  25.       }
  26.  
  27.       /* print a ruler to make things clearer   */
  28.  
  29.       puts("");
  30.       for (i = 0; i < 71; ++i)
  31.       {
  32.             if (i == 10 * (i / 10))
  33.                   putnum(i / 10);
  34.             else  putchar(' ');
  35.       }
  36.       puts("");
  37.       for (i = 0; i < 71; ++i)
  38.             putnum(i % 10);
  39.       return 0;
  40. }
  41.  
  42. /*
  43.  
  44. RESULTS:
  45.  
  46.                Student Name                    Test Score
  47.  
  48.                Fuddrucker, Aloysius                -10
  49.                Stout, Bob                           70
  50.                Ritchie, Dennis                     200
  51.  
  52. 0         1         2         3         4         5         6         7
  53. 01234567890123456789012345678901234567890123456789012345678901234567890
  54.  
  55. */
  56.